﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BL.DataAccess;
using BL.Entities;


namespace zCO
{
    public partial class frmListarLiquidaciones : Form
    {
        string NombreObraSocial = ""; //se utiliza para poner el detalle en la liquidacion del profesional
        string starkey = " ";
        string halfStarkey = " ";
        double porc =1; //Porcentage que se utiliza para la liquidacion
        public frmListarLiquidaciones()
        {
            InitializeComponent();
            porc = Cuota.GetValorPorcentaje();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ColeccionObrasSociales obras = new ColeccionObrasSociales();
            if (textBox1.Text != "")
            {
                obras = ColeccionObrasSociales.LoadObraSocial(textBox1.Text);
            }
            foreach (ObraSocial o in obras)
            {

                textBox1.AutoCompleteCustomSource.Add(o.NombreObra);

            }

        }

        //private void textBox2_TextChanged(object sender, EventArgs e)
        //{
        //    //ColeccionProfesionales prof = new ColeccionProfesionales();
        //    //if (textBox2.Text != "")
        //    //{
        //    //    prof = ColeccionProfesionales.LoadProfesional(textBox2.Text);
        //    //}
        //    //foreach (Profesionales p in prof)
        //    //{

        //    //    textBox2.AutoCompleteCustomSource.Add(p.Apellido.ToString());

        //    //}


        //}

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "") {
                if (ColeccionObrasSociales.Current.GetByNombre(textBox1.Text) != null) { 
                    NombreObraSocial = textBox1.Text;
                }
            
            }else{
                    NombreObraSocial = "";
            }
            CargarGrilla();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            UpdateGrilla();

        }
        private void CargarGrilla()
        {
            ColeccionIntervenciones inter = new ColeccionIntervenciones();
            DataTable dt = new DataTable();
            dataGridView1.Rows.Clear();
            string NombreObra = textBox1.Text;
            string matricula = textBox2.Text;
            string paciente = textBox3.Text;
            DateTime fechaIni = dateTimePicker1.Value.Date;
            DateTime fechaEnd = dateTimePicker2.Value.Date;
            string estado = comboBox1.Text;
            string abonada = comboBox2.Text;
            bool refact = checkRefacturacion.Checked;
            Double totalFinal = 0;
            inter = ColeccionIntervenciones.LoadObraSocial(NombreObra, matricula,paciente, estado, abonada, fechaIni, fechaEnd,refact);
        
            int count = inter.Count;
            int index = 0;
            Double total = 0;
            foreach (Intervenciones i in inter)
            {


                if (i.Abonada == "True")
                {
                    i.Abonada = "Si";
                }
                else
                {
                    i.Abonada = "No";
                }

                if (index != 0 && dataGridView1.Rows[index - 1].Cells[1].Value.ToString() != starkey && i.Matricula != Int32.Parse(dataGridView1.Rows[index - 1].Cells[1].Value.ToString()))
                {
                    string mat = dataGridView1.Rows[index - 1].Cells[1].Value.ToString();

                    dataGridView1.Rows.Add(starkey, starkey, halfStarkey + halfStarkey, halfStarkey + halfStarkey, starkey, starkey, starkey, "TOTAL " + dataGridView1.Rows[index - 1].Cells[2].Value.ToString(), total, starkey, starkey, starkey, starkey);
                    total = 0;
                    for (int a = 0; a < 13; a++)
                    {
                        if (a == 7 || a == 8)
                            dataGridView1.Rows[index].Cells[a].Style.BackColor = Color.ForestGreen;
                        else
                            dataGridView1.Rows[index].Cells[a].Style.BackColor = Color.DarkGray;

                    }
                    index++;
                }
                dataGridView1.Rows.Add(i.NombreObraSocial, i.Matricula, i.Apellido + i.Nombre, i.ApellidoAfiliado.ToUpper() + " " + i.NombreAfiliado, i.NumeroAfiliado, i.IdPrestacion, i.Elemento, i.Cara, i.Arancel, i.Estado, i.Abonada, i.Fecha.Substring(0, 10), i.Detalle, i.IdIntervencion);
                total += Double.Parse(dataGridView1.Rows[index].Cells[8].Value.ToString());
                index++;
            }
            if (index != 0 && dataGridView1.Rows[index - 1].Cells[1].Value.ToString() != starkey)
            {
                string matt = dataGridView1.Rows[index - 1].Cells[1].Value.ToString();

                dataGridView1.Rows.Add(starkey, starkey, halfStarkey + halfStarkey, halfStarkey + halfStarkey, starkey, starkey, starkey, "TOTAL " + dataGridView1.Rows[index - 1].Cells[2].Value.ToString(), total, starkey, starkey, starkey, starkey);
                for (int a = 0; a < 13; a++)
                {
                    if (a == 7 || a == 8)
                        dataGridView1.Rows[index].Cells[a].Style.BackColor = Color.ForestGreen;
                    else
                        dataGridView1.Rows[index].Cells[a].Style.BackColor = Color.DarkGray;

                }
            }
            foreach (Intervenciones i in inter)
            {
                totalFinal += i.Arancel;

            }
            if (index != 0)
            {
                dataGridView1.Rows.Add(starkey, starkey, halfStarkey + halfStarkey, halfStarkey + halfStarkey, starkey, starkey, starkey, "TOTAL GENERAL", totalFinal, starkey, starkey, starkey, starkey);
                for (int a = 0; a < 13; a++)
                {
                    if (a == 7 || a == 8)
                        dataGridView1.Rows[index + 1].Cells[a].Style.BackColor = Color.IndianRed;
                    else
                        dataGridView1.Rows[index + 1].Cells[a].Style.BackColor = Color.IndianRed;

                }
            }
        }

        private void UpdateGrilla()
        {
            
            ColeccionIntervenciones inter = new ColeccionIntervenciones();
            int matr;
            string numafi;
            int idPrest;
            string elem;
            string cara;
            string nomO;
            string abnd;
            string estd;
            DateTime fech;
            int idInt;
            int total;
            DateTime fechaLiq;
            string detalle;
            List<int> idLiquidaciones = new List<int>();
            int idLiquidacion = 0;
            for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
            {

                if (dataGridView1.Rows[i].Cells[0].Value.ToString() != starkey) {
                    double  monto = double.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString());
                    Movimiento m = new Movimiento();
                    if (NombreObraSocial != "")
                    {
                        m.Concepto = "Cobro por Intervenciones (" + NombreObraSocial + ") " + DateTime.Now.ToString("MMMM yyyy");
                    }
                    else
                    {
                        m.Concepto = "Cobro por Intervenciones " + DateTime.Now.ToString("MMMM yyyy");
                    }
                    m.Fecha = DateTime.Now;
                    m.Monto = monto;// -(monto * porc / 100);
                    m.Profesional = ColeccionProfesionales.Current.GetByMatricula(dataGridView1.Rows[i].Cells[1].Value.ToString())[0];

                    //change i-1 insted of i-2.
                    //m.Profesional = ColeccionProfesionales.Current.GetByMatricula(dataGridView1.Rows[i - 2].Cells[1].Value.ToString())[0];
                    if (i == dataGridView1.Rows.Count - 2)
                    {
                        m.Profesional = ColeccionProfesionales.Current.GetByMatricula(dataGridView1.Rows[i - 2].Cells[1].Value.ToString())[0];
                    }
                    else {
                        m.Profesional = ColeccionProfesionales.Current.GetByMatricula(dataGridView1.Rows[i - 1].Cells[1].Value.ToString())[0];
                    }
                    


                    idLiquidacion = m.Save();
                    idLiquidaciones.Add(idLiquidacion);

                }
            }
            idLiquidacion = idLiquidaciones[0];
            int puntero = 0;

            for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
            {
                if (dataGridView1.Rows[i].Cells[0].Value.ToString() != starkey)
                {
                    nomO = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    matr = Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString());
                    numafi = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    idPrest = Int32.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString());
                    elem = dataGridView1.Rows[i].Cells[6].Value.ToString();
                    cara = dataGridView1.Rows[i].Cells[7].Value.ToString();
                    estd = dataGridView1.Rows[i].Cells[9].Value.ToString();
                    abnd = "Si";
                    fech = DateTime.Parse(dataGridView1.Rows[i].Cells[11].Value.ToString());
                    detalle = dataGridView1.Rows[i].Cells[12].Value.ToString();
                    idInt = Int32.Parse(dataGridView1.Rows[i].Cells[13].Value.ToString());
                    fechaLiq= DateTime.Parse(DateTime.Now.ToShortDateString());

                    ColeccionIntervenciones.UpdateIntervencionLiquidada(nomO, matr, numafi, idPrest, elem, cara, estd, abnd, fech, detalle, idInt,idLiquidacion,fechaLiq);
                    if (i > 0)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value.ToString() != dataGridView1.Rows[i - 1].Cells[0].Value.ToString() && dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
                        {
                            
                        }
                    }
                }
                else {
                    puntero++;
                    if (puntero < idLiquidaciones.Count)
                    {
                        idLiquidacion = idLiquidaciones[puntero];
                    }
                
                }
            }

            

            CargarGrilla();
        
        
        }

        private void frmListaPrestaciones_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void cmdPrint_Click(object sender, EventArgs e)
        {
            Excel.Imprimir(dataGridView1, true);
        }

        private void cmdExcel_Click(object sender, EventArgs e)
        {
            Excel.export(dataGridView1, true);
        }

        private void frmListarLiquidaciones_Load(object sender, EventArgs e)
        {


            textBox2.AutoCompleteMode = AutoCompleteMode.Append;
            textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox3.AutoCompleteMode = AutoCompleteMode.Append;
            textBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;
           
            foreach (var item in ColeccionProfesionales.Current)
            {
                if (item.Activo == true)
                {
                    textBox2.AutoCompleteCustomSource.Add(item.Apellido.ToUpper());
                }
            }
            foreach (var item in ColeccionPacientesForm.Current)
            {

                textBox3.AutoCompleteCustomSource.Add(item.Apellido.ToUpper());
            }
        }

       




    }
}
